<?php

namespace App\Modules\Admin\Entities\Member;
use App\Models\Model;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use Carbon\Carbon;

class UserScoreModel extends Model
{
    protected $table = 'user_scores';
    protected $is_delete=0;

    //-1：提现被拒绝退回 0：获得 1：提现

    const  TAKE = 1;


    public function water_device_info(){
        return $this->hasOne(WaterDeviceModel::class,'water_id','water_id')->select(['water_id','water_title']);
    }
    public function user_info(){
        return $this->hasOne(UsersModel::class,'id','user_id')->select('id','nickname');
    }



    public function getUserScoreStatistic($params){

        $query = $this->query()->selectRaw("sum(score) AS value, from_unixtime(gift_time,'%Y-%m-%d') as times");
        $query = $query->where('user_id', $params['user_id']);
//        if $params['apparatus_id'])
        $query = $query->where('type', $params['type']);
        $query = $query->where('is_delete', 0);
        $query = $query->whereBetween($this->table . '.gift_time', [$params['start_time'], $params['end_time']]);
//        $query = $query->whereRaw("gift_time >= {$params['start_time']}");
//        $query->groupBy('times');
        $query = $query->groupBy( 'times' );
//        $query = $query->orderBy( 'times','asc' );
        return  $query->get()->toArray();
    }
}
